﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using FinanceCalculators;
using HomeBuyerTools.FinanceProperties;
using Microsoft.Phone.Tasks;
using CommonControls;
using LocalSettings;
using HomeBuyerTools.Views.RentVSBuy;


namespace HomeBuyerTools.Views.Result
{
    public partial class ResultPage : PhoneApplicationPage
    {
        List<TotalExpensiveProperties> ListTotal = new List<TotalExpensiveProperties>();
        List<Trems> RemoveListExtra = new List<Trems>();
        public ResultPage()
        {
            InitializeComponent();
            IsolatedSettings.saveSettings("Msuid", "33460");
            IsolatedSettings.saveSettings("CheckMicrosoftAds", "d9bccf2b-28a2-4c04-bb4b-acfeda0677ac");
           // AboutUs objAboutUs = new AboutUs( "/Images/Pivot/Background.jpg");
            LoadPanoramaThemes();
        }
        AboutUs objAboutUs = new AboutUs();
       
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
           Visibility darkBackgroundVisibility = (Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"];

           if (darkBackgroundVisibility == Visibility.Visible)
           {
               //Theme is Dark
           }

           else
           {
               //Theme is Light
               txtAgentCommission.MouseEnter += txtAgentCommission_MouseEnter;
               txtAgentCommission.MouseLeave += txtAgentCommission_MouseLeave;
               txtHomeCoastCal1.MouseEnter += txtHomeCoastCal1_MouseEnter;
               txtHomeCoastCal1.MouseLeave += txtHomeCoastCal1_MouseLeave;
           }
   
            RentVSBuy.RentVSBuy cal = new RentVSBuy.RentVSBuy();
            cal.Calculate();
            Settings totalExp=new Settings();
            tbECofRenting.Text = App.NetRentCost.ToString();
            tbECofBuying.Text = App.netBuyCost;
            tbSummary.Text = "Summary  :"    + "   " + App.summary;
            //tbtimeperiods.Text = "Period of time  :   " + "  " + App.FromDate + "    To   " + App.Todate;
            //logoandtitle();
            //homeprojectionresult();
            compareloanbyrate();
            compareloanbyterm();
            //homeinsurance();
            mortgagepayment();
            ListTotal = totalExp.loadTotalExpensive();
           // lbxAboutUs.ItemsSource = objAboutUs.LoadAboutUsItems();
            TotalExpensiveCalculation();
            TotalSalesPrice();
            string sectionId="";
            if(NavigationContext.QueryString.TryGetValue("sectionId",out sectionId))
            {
                if(sectionId=="1")
                    panoramaMain.DefaultItem = MortgagePayment;
                if(sectionId == "2")
                    panoramaMain.DefaultItem = CRate;
                if (sectionId == "3")
                    panoramaMain.DefaultItem = CTerm;
                if (sectionId == "4")
                    panoramaMain.DefaultItem = rentvsbuy;
            }
            
        }

        void txtHomeCoastCal1_MouseLeave(object sender, MouseEventArgs e)
        {
            txtHomeCoastCal1.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtHomeCoastCal1_MouseEnter(object sender, MouseEventArgs e)
        {
            txtHomeCoastCal1.Foreground = new SolidColorBrush(Colors.White);
        }

        void txtAgentCommission_MouseLeave(object sender, MouseEventArgs e)
        {
            txtAgentCommission.Foreground = new SolidColorBrush(Colors.Black);
        }

        void txtAgentCommission_MouseEnter(object sender, MouseEventArgs e)
        {
            txtAgentCommission.Foreground = new SolidColorBrush(Colors.White);
        }
        private void logoandtitle()
        {
           
        }
        //private void homeprojectionresult()
        //{
        //    string phm = "";
        //    string hp = "";
        //    string lp = "";
        //    string value = "";
        //    double Rate = 20 / 100;
        //    double Highintrest = 0.0;
        //    double LowIntrest = 0.0;
        //    double ProjectedHomeValue = 100;
        //    double HighProjection = 300;
        //    double LowProjection = 200;
        //    double value1 = 100;

        //    if (NavigationContext.QueryString.TryGetValue("phm", out phm))
        //    {

        //        txtProjectedHomeValue.Text = phm.ToString();
        //    }
        //    else
        //    {
        //        ProjectedHomeValue = ProjectedHomeValue + (ProjectedHomeValue * Rate);
        //        ProjectedHomeValue = Math.Round(ProjectedHomeValue, 4);
        //        txtProjectedHomeValue.Text = ProjectedHomeValue.ToString();
        //    }
        //    if (NavigationContext.QueryString.TryGetValue("hp", out hp))
        //    {

        //        txtHighProjection.Text = hp.ToString();
        //    }
        //    else
        //    {

        //        HighProjection = HighProjection + (HighProjection * Highintrest);
        //        HighProjection = Math.Round(HighProjection, 4);
        //        txtHighProjection.Text = HighProjection.ToString();
        //    }
        //    if (NavigationContext.QueryString.TryGetValue("lp", out lp))
        //    {

        //        txtLowProjection.Text = lp.ToString();
        //    }
        //    else
        //    {
        //        LowProjection = LowProjection + (LowProjection * LowIntrest);
        //        LowProjection = Math.Round(LowProjection, 4);
        //        txtLowProjection.Text = LowProjection.ToString();
        //    }

        //    if (NavigationContext.QueryString.TryGetValue("value", out value))
        //    {

        //        tbProjectedHome.Text = value + "$" + "growing annualy :";
        //    }
        //    else
        //    {
        //        tbProjectedHome.Text = value1 + "$" + "growing annualy :  ";
        //    }
        //}
        private void compareloanbyrate()
        {
            string mp = "";
            string tm = "";
            string ir = "";
            string cc = "";
            if (NavigationContext.QueryString.TryGetValue("mp", out mp))
            {

            }
            else
                mp = "648";

            if (NavigationContext.QueryString.TryGetValue("tm", out tm))
            {

            }
            else
                tm = "10";

            if (NavigationContext.QueryString.TryGetValue("ir", out ir))
            {

            }
            else
                ir = "3.5";

            if (NavigationContext.QueryString.TryGetValue("cc", out cc))
            {

            }
            else
                cc = "1/8";


            double mprinciple = Convert.ToDouble(mp);
            double inRate = Convert.ToDouble(ir);
            double NoOfMonths = Convert.ToDouble(tm);
            double interestRatio;

            if (cc == "1/8%")
            {
                interestRatio = 1.0 / 8;
            }
            else  if (cc == "1/4%")
            {
                interestRatio = 1.0 / 4;
            }
            else if (cc == "3/8%")
            {
                interestRatio = 3.0 / 8;
            }
            else  if (cc == "1/2%")
            {
                interestRatio = 1.0 / 2;
            }
            else if (cc == "5/8%")
            {
                interestRatio = 5.0 / 8;
            }
            else  if (cc == "3/4%")
            {
                interestRatio = 3.0 / 4;
            }
            else  if (cc == "7/8%")
            {
                interestRatio = 7.0 / 8;
            }
            else interestRatio = 1.0;
           
            double startInterestRate = Math.Floor(inRate);

            List<double> RateList = new List<double>();
            for (int i = 1; i <= 5; i++)
            {
                RateList.Add(startInterestRate);
                startInterestRate = startInterestRate + interestRatio;
            }

            List<CompareLoanByRate> objRateList = new List<CompareLoanByRate>();

            foreach (double indRate in RateList)
            {
                CompareLoanByRate objRate = new CompareLoanByRate();
                double totalinterest = 0.0;
                double MonthlyPayment = 0.0;
                double t=(indRate / 100);
                double f = t / 12;
                double k=(Math.Pow(1 / (1 +f), NoOfMonths));
                double t1=1-k;
                double Interest = Math.Floor(mprinciple *f);
                double payment = Math.Round((mprinciple *f) /t1,2)/10;
               // double payment = Math.Round(payment1);
                double principle = Math.Floor(payment - Interest);
                double balance = Math.Floor(mprinciple - principle);
                MonthlyPayment = payment-0.5;
                totalinterest = Interest;
                for (int i = 1; i <= NoOfMonths; i++)
                {
                    Interest = Math.Floor(balance * (indRate / 100));
                    payment = Math.Floor((balance * (indRate / 100 / 12)) / (1 - (Math.Pow(1 / (1 + (indRate / 100 / 12)), (NoOfMonths - i)))));
                    principle = Math.Floor(payment - Interest);
                    balance = Math.Floor(balance - principle);
                    totalinterest = totalinterest+Interest;
                    totalinterest = Math.Floor(totalinterest);
                }
                objRate.InterestRate = Math.Round(indRate, 3).ToString();
                objRate.MonthlyPayment =Math.Round(MonthlyPayment,0).ToString();
                 string interset="";
                interset = Math.Floor(((totalinterest))).ToString();
                objRate.TotalInterest = interset ;
                objRate.TotalPrinciple = mprinciple.ToString();
                objRate.TotalPayments = (Math.Round(mprinciple + Convert.ToDouble(interset),0)).ToString();

                objRateList.Add(objRate);
            }
            lbxComLoanByRate.ItemsSource = objRateList;
        }
        private void compareloanbyterm()
        {
            string pb = "";
            string ir = "";
            if (NavigationContext.QueryString.TryGetValue("pb", out pb))
            {
            }
            else

                pb = "648";

            if (NavigationContext.QueryString.TryGetValue("ir", out ir))
            {

            }
            else

                ir = "3";

            double princbalance = Convert.ToDouble(pb);
            double interestrate = Convert.ToDouble(ir);

            List<CompareLoanByTerm> objTermList = new List<CompareLoanByTerm>();

            int StartYear = 10;
            List<int> yearsDuration = new List<int>();
            for (int i = 1; i <= 5; i++)
            {
                yearsDuration.Add(StartYear);
                StartYear = StartYear + 5;
                //startInterestRate = startInterestRate + interestRatio;
            }


            foreach (int yrs in yearsDuration)
            {
                CompareLoanByTerm objTerm = new CompareLoanByTerm();
                double totalinterest = 0.0;
                double MonthlyPayment = 0.0;
                double Interest = Math.Round(princbalance * (interestrate / 100 / 12), 0);
                double payment = Math.Round((princbalance * (interestrate / 100 / 12)) / (1 - (Math.Pow(1 / (1 + (interestrate / 100 / 12)), yrs * 12))), 0);
                double principle = Math.Round(payment - Interest, 0);
                double balance = Math.Round(princbalance - principle, 0);
                MonthlyPayment = payment;

                totalinterest = Interest;
                for (int i = 1; i <= yrs * 12; i++)
                {
                    Interest = Math.Round(balance * (interestrate / 100 / 12), 0);
                    payment = Math.Round((balance * (interestrate / 100 / 12)) / (1 - (Math.Pow(1 / (1 + (interestrate / 100 / 12)), (yrs * 12 - i)))), 0);
                    principle = Math.Round(payment - Interest, 0);
                    balance = Math.Round(balance - principle, 0);
                    totalinterest = totalinterest + Interest;
                }
                //objTerm.InterestRate = indRate.ToString();
                objTerm.TermOfMortgage = yrs.ToString();
                objTerm.MonthlyPayments = MonthlyPayment.ToString();
                objTerm.TotalInterest = totalinterest.ToString();
                objTerm.TotalPrinciple = princbalance.ToString();
                objTerm.TotalPayments = (Math.Round(princbalance + totalinterest,0)).ToString();

                objTermList.Add(objTerm);
            }
            lbxComLoanByTerm.ItemsSource = objTermList;

        }
        //public void homeinsurance()
        //{
        //    string tl = "";
        //    string ir1 = "";
        //    string IdentityTheftLevel = "";

        //    if (NavigationContext.QueryString.TryGetValue("tl", out tl))
        //    {

        //    }
        //    else
        //    {
        //        tl = "LessThan$100,000";
        //    }
        //    if (NavigationContext.QueryString.TryGetValue("ir1", out ir1))
        //    {
        //    }
        //    else
        //    {
        //        ir1 = "LowTolerance";
        //    }
        //    if (NavigationContext.QueryString.TryGetValue("IdentityTheftLevel", out IdentityTheftLevel))
        //    {
        //    }
        //    else
        //    {
        //        IdentityTheftLevel = "Coverage";
        //    }
        //    if (tl == "LessThan$100,000")
        //        txtWorth.Text = "$100,000";
        //    else if (tl == "$100,000-$300,000")
        //        txtWorth.Text = "$300,000";
        //    else
        //        txtWorth.Text = "$500,000";

        //    if (ir1 == "LowTolerance")
        //        txtRiskTolerance.Text = "$500";
        //    else if (ir1 == "MediumTolerance")
        //        txtRiskTolerance.Text = "$1,000";
        //    else
        //        txtRiskTolerance.Text = "$2,500";
        //    txtIdentitythieft.Text = IdentityTheftLevel;
            
        //}
       
       
        public void mortgagepayment()
        {
            string LA = "";
            string IR = "";
            string TL = "";
            string Pri = "";
            string HC = "";
          
            double IntrestRate;
            double LoanAmount;
            double Price=0.0;
            double TermOfLoan;
            double Month;
            double HomeCost;
          
            double DownPayment;
           
            if (NavigationContext.QueryString.TryGetValue("LA", out LA))
            {
                LoanAmount = Convert.ToDouble(LA);
                Appsettings.LoanAmount = LoanAmount;
            }
            else
            {
                if (Appsettings.LoanAmount == 0)
                {
                    LoanAmount = Convert.ToDouble(412000);
                    Appsettings.LoanAmount = LoanAmount;
                }
                else
                {
                    LoanAmount = Appsettings.LoanAmount;
                }
            }
            if (NavigationContext.QueryString.TryGetValue("HC", out HC))
            {
                HomeCost = Convert.ToDouble(HC);
                Appsettings.HomeCost = HomeCost;
            }
            else
            {
                if (Appsettings.HomeCost == 0)
                {
                    HomeCost = Convert.ToDouble(400000);
                    Appsettings.HomeCost = HomeCost;
                }
                else
                {
                    HomeCost = Appsettings.HomeCost;
                }
            }
            if (NavigationContext.QueryString.TryGetValue("IR", out IR))
            {
                IntrestRate = Convert.ToDouble(IR);
                Appsettings.IntrestRate = Appsettings.IntrestRate;
            }
            else
            {
                if (Appsettings.IntrestRate == 0)
                {
                    IntrestRate = 3.5;
                    Appsettings.IntrestRate = IntrestRate;
                }
                else
                {
                    IntrestRate = Appsettings.IntrestRate;
                }
            }
            if (NavigationContext.QueryString.TryGetValue("TL", out TL))
            {
                TermOfLoan = Convert.ToDouble(TL);
                Appsettings.TermOfLoan = TermOfLoan;
            }
            else
            {
                if (Appsettings.TermOfLoan == 0)
                {
                    TermOfLoan = 360;
                    Appsettings.TermOfLoan = TermOfLoan;
                }
                else
                {
                    TermOfLoan = Appsettings.TermOfLoan;
                }
            }
            if (NavigationContext.QueryString.TryGetValue("Pri", out Pri))
            {
                Price = Convert.ToDouble(Pri);
                DownPayment =Convert.ToDouble( Pri) * 0.2;
                Appsettings.Price = Price;
            }
            else
            {
                if (Appsettings.Price == 0)
                {
                    Price = 573000;
                    Appsettings.Price = Price;
                }
                else
                {
                    Price = Appsettings.Price;
                }
                DownPayment = Price * 0.2;
            }

            App.TotalPrice = HomeCost;
            txtHomeCoastCal.Text = HomeCost.ToString();
            txtHomeCoastCal1.Text = HomeCost.ToString();
            tblkdownpayment.Text = DownPayment.ToString();
            double payment =Math.Round(LoanAmount * (IntrestRate / 100 / 12 / (1 - Math.Pow((1 + IntrestRate / 100 / 12), (-TermOfLoan)))),2);
            tblkpayment.Text = Convert.ToString(payment);
            
        //IntrestRate = Convert.ToDouble(IR);
        //    TermOfLoan = Convert.ToDouble(TL);
        //    Price = Convert.ToDouble(Pri);
            //LoanPayment = Convert.ToDouble(LP);
           

            //double Payment = 1850.06
            List<Trems> p = new List<Trems>();
            DateTime Dt = default(DateTime);
            if (Appsettings.RegstrationDate !="")
            {
                 Dt = Convert.ToDateTime(Appsettings.RegstrationDate);
            }
            for (int i = 1; i <= (TermOfLoan); i++)
            {
                
                Trems t = new Trems();
                if (p.Count == 0)
                {
                    t.PeriodDate = Dt;
                }
                else
                {
                    t.PeriodDate = Dt.AddMonths(i-1);
                }
                t.Month = i;
                double Intrest = (LoanAmount) * (IntrestRate) / (1200);
                t.Intrest = Math.Round(Intrest, 2);
                double Principal = payment - Intrest;
                t.Principal = Math.Round(Principal, 2);
                double Balance = (LoanAmount) - Principal;
                t.Balance = Math.Round(Balance, 2);
                LoanAmount = Math.Round(Balance, 2);
               
                p.Add(t);
                RemoveListExtra.Add(t);
            }
            listBox1.ItemsSource = p;
        }

     

        //MortageSaveData savadata = new MortageSaveData();
        //   listBox1.ItemsSource = savadata.LoadDetails();
        //}
        private void Imagemortgage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/MortagePayment/MortagePayment.xaml", UriKind.Relative));
        }
        private void Imagehome_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/HomeValueProjection/HomeValueProjection.xaml", UriKind.Relative));
        }
        private void Imagecrate_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/CompareLoansByRate/CompareLoansByRate.xaml", UriKind.Relative));
        }
        private void Imagecterm_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/CompareLoansByTerm/CompareLoansByTerm.xaml", UriKind.Relative));
        }
        //private void Imageinsurance_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        //{
        //    NavigationService.Navigate(new Uri("/Views/HomeInsurance/HomeInsurance.xaml", UriKind.Relative));
        //}

        //private void twitterImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        //{
        //    SbTwitter.Completed += new EventHandler(SbTwitter_Completed);
        //}
        //void SbTwitter_Completed(object sender, EventArgs e)
        //{
        //    LoadBrowserTask("http://bit.ly/exHPzZ");
        //}

        //private void youtubeImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        //{
        //    SbYoutube.Completed += new EventHandler(SbYoutube_Completed);
        //}
        //void SbYoutube_Completed(object sender, EventArgs e)
        //{
        //    LoadBrowserTask("http://bit.ly/eYf4xG");
        //}

        //private void blogImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        //{
        //    SbBlogger.Completed += new EventHandler(SbBlogger_Completed);
        //}

        //void SbBlogger_Completed(object sender, EventArgs e)
        //{
        //    LoadBrowserTask("http://bit.ly/hpoWQX");
        //}

        //private void facebookImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        //{
        //    SbFacebook.Completed += new EventHandler(SbFacebook_Completed);
        //}
        //void SbFacebook_Completed(object sender, EventArgs e)
        //{
        //    LoadBrowserTask("http://on.fb.me/eytwPa");
        //}
        private void LoadBrowserTask(string url)
        {
            WebBrowserTask webBrowserTask = new WebBrowserTask();
            webBrowserTask.URL = url;
            webBrowserTask.Show();

           
        }
        //private void lbxAboutUs_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    if (lbxAboutUs.SelectedIndex != -1)
        //    {
        //        int SelIndex = lbxAboutUs.SelectedIndex;
        //        lbxAboutUs.SelectedIndex = -1;
        //        switch (SelIndex)
        //        {
        //            case 0:
        //                //rate this app
        //                MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
        //                marketplaceReviewTask.Show();
        //                break;
        //            case 1:
        //                //share this app
        //                EmailComposeTask emailcomposertask = new EmailComposeTask();
        //                emailcomposertask.To = "";
        //                emailcomposertask.Subject = "Activity Tracker App";
        //                string lnk = "I have recently found a very good productivity app that I would like to share with you.\n";
        //                lnk = lnk + "Below is the link of the app.\n\n\n";
        //                lnk = lnk + "\n\n\nFrom Windows PC\n";
        //                lnk = lnk + "http://social.zune.net/redirect?type=phoneApp&id=ebe21744-8623-e011-854c-00237de2db9e";
        //                lnk = lnk + "From Windows Phone 7.\n";
        //                lnk = lnk + "zune://navigate/?appID=ebe21744-8623-e011-854c-00237de2db9e";
        //                emailcomposertask.Body = lnk;
        //                emailcomposertask.Show();
        //                break;
        //            case 2:
        //                //related apps
        //                MarketplaceSearchTask marketplaceSearchTask = new MarketplaceSearchTask();
        //                marketplaceSearchTask.SearchTerms = "lartsoft";
        //                marketplaceSearchTask.Show();
        //                break;
        //            case 3:
        //                //Feedback
        //                NavigationService.Navigate(new Uri("/Views/Feedback.xaml", UriKind.Relative));
        //                break;
        //        }
        //    }
        //}

        //private void HelpImage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        //{
        //    SbHelp.Completed += new EventHandler(SbHelp_Completed);
        //}
        //void SbHelp_Completed(object sender, EventArgs e)
        //{
        //    LoadBrowserTask("http://bit.ly/gpGImW");

        //}
        private void Imagerentvsbuy_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/RentVSBuy/RentVSBuy.xaml", UriKind.Relative));
        }
        public void TotalExpensiveCalculation()
        {
             List<TotalExpensiveProperties> tt = new List<TotalExpensiveProperties>();
             try
             {
              
                 if (ListTotal.Count != 0)
                 {
                     DateTime d;
                     DateTime e;
                     DateTime f;
                     double t = 0;
                     double t1 = 0;
                     double sum = 0;
                     double result = 0;
                     double day = 0;
                     double i = 0;
                     double t3 = 0;
                     double t4 = 0;
                     List<TotalExpensiveProperties> TotalMonthExp = new List<TotalExpensiveProperties>();
                     foreach (TotalExpensiveProperties s in ListTotal.ToArray())
                     {
                         //sum += Convert.ToDouble(s.Amount);
                         tt.Add(s);
                         d = s.FromDateTime;
                         e = s.ToDate;
                         DateTime CurrentDateForExpenses = DateTime.Now;
                         //i = Math.Round((e - d).TotalDays, 0);
                         f = DateTime.Now;
                         t1 = Math.Round((e - d).TotalDays, 0);
                         if (t1 == 31)
                         {
                             t1 = t1 - 1;
                         }
                         int result1 = DateTime.Compare(d, e);
                         int k3 = result1;
                         sum = sum + (Convert.ToDouble(s.Amount));
                         if (CurrentDateForExpenses.Year == e.Year)
                         {
                           
                             if (CurrentDateForExpenses.Month == e.Month)
                             {
                                 TotalMonthExp.Add(s);
                                 result = result + Convert.ToDouble(s.Amount);
                                 int Days = CurrentDateForExpenses.Day - 1;
                                 if (Days== e.Day)
                                 {
                                     sum = sum - Convert.ToDouble(s.Amount);
                                     result = result - Convert.ToDouble(s.Amount);
                                     //int k = tt.IndexOf(s);
                                     //tt.RemoveAt(k);
                                     int k = TotalMonthExp.IndexOf(s);
                                     TotalMonthExp.RemoveAt(k);
                                     Settings st = new Settings();
                                     ListTotalExpensive.ItemsSource = st.RemovieItems(s.ExpensiveName, s.Amount);
                                 }
                             }
                         }
                        
                         //    t4 = t1 / 30;
                         //    sum = sum + (Convert.ToDouble(s.Amount) * t4);
                         //    t3 = t1;
                         //    t = Math.Round((f - d).TotalDays, 0);
                         //    // t1 = Math.Round((f - d).TotalDays, 0) - 1;
                         //    if (t == 30 || t == 60 || t == 90 || t == 120 || t == 150 || t == 180 || t == 210 || t == 240 || t == 270 || t == 300 || t == 330 || t == 360 || t == 365)
                         //    {
                         //        if (t1 != t)
                         //        {
                         //            t3 = t1 - t;
                         //        }
                         //        sum = sum - Convert.ToDouble(s.Amount);
                         //        // MessageBox.Show(Convert.ToString(s));
                         //        int k = tt.IndexOf(s);
                         //        tt.RemoveAt(k);
                         //        Settings st = new Settings();
                         //        ListTotalExpensive.ItemsSource = st.RemovieItems(s.ExpensiveName, s.Amount);
                         //    }
                         //}
                         //day = sum / 30;
                         //result = day * t3;
                         App.TotalExpSum =result;
                         tbsum.Text = "Total                       :     " + Math.Round(result, 2);
                         tbResult.Text = " Period of Time Ammount  :   " + Math.Round(sum, 2).ToString();
                     }
                    
                     //else
                     //{
                     //    tbsum.Text = "Total                       :     " + 0;

                     //    tbResult.Text = "Period of Time Ammount   :   " + 0;
                     //}
                    // ListTotalExpensive.ItemsSource = tt;
                     ListTotalExpensive.ItemsSource = TotalMonthExp;
                 }
             }
             catch (Exception ex)
             {
             }
        }

        public void TotalSalesPrice()
        {
            string RD = string.Empty;
            string ACH = string.Empty;
            string TP = string.Empty;
            string AC = string.Empty;
            string RegistrationDate = string.Empty;
            double AgentCommissionHouse = 0;
            double TotalPrice = 0;
            double AgentCommission = 0;
            double ExpSum = 0;
            double TotalExpSum = 0;
           
       
            double TotalSalesPrice = 0;

            if (Appsettings.DiffMonths != 0)
            {
                for (int l = 1; l <= Appsettings.DiffMonths; l++)
                {
                    double exprincipal = RemoveListExtra.ElementAt(0).Principal;
                    double ExpInterst = RemoveListExtra.ElementAt(0).Intrest;
                    ExpSum = exprincipal + ExpInterst;
                    TotalExpSum = TotalExpSum + ExpSum;
                    RemoveListExtra.RemoveAt(0);
                }
            }
            foreach (Trems tr in RemoveListExtra)
            {
                DateTime CurrentDate = DateTime.Now;
                int year = tr.PeriodDate.Year;
                int Month = tr.PeriodDate.Month;
                int Day=tr.PeriodDate.Day;
                double LoanTream=0;
                if (CurrentDate.Year== year)
                {
                    if (CurrentDate.Month == Month)
                    {
                        if (CurrentDate.Day == Day)
                        {
                            if (Appsettings.CompareMonth != tr.Month)
                            {
                                Appsettings.CompareMonth = tr.Month;
                                Appsettings.LoanTream = Appsettings.LoanTream + tr.Principal + tr.Intrest;
                                //TotalSalesPrice = LoanTream + App.TotalExpSum + AgentCommissionHouse;
                                //Appsettings.TotalSumForSalesPrice = Appsettings.TotalSumForSalesPrice + TotalSalesPrice;
                            }
                        }
                    }

                }

            }
            double TotalExpLoan = Appsettings.LoanTream + TotalExpSum;
            tbLoan.Text = Math.Round(TotalExpLoan, 2).ToString();
            txtTotalAgentCommission.Text = Math.Round(Appsettings.AgentCommissionHouse, 2).ToString();
            txtAgentCommission.Text = Appsettings.AgentCommission.ToString();
            tbTotalExp.Text = Math.Round(App.TotalExpSum, 2).ToString();
            double tbloan1 = Math.Round(TotalExpLoan, 2);
            double AgentCommission1 = Math.Round(Appsettings.AgentCommissionHouse, 2);
            double TotalExp1 = Math.Round(App.TotalExpSum, 2);
            double res = tbloan1 + AgentCommission1 + TotalExp1;
            tbESP.Text = res.ToString();
        }
        private void LoadPanoramaThemes()
        {
   
            //panoramaMain.Background = App.PanaromaBg;
            //imgLogo.Source = App.PanaramaLogo;
            //imgTitle.Source = App.PanaramaTitle;
            //tblkHeader.Text = IsolatedSettings.getSettingsStringValue("prmh");
        }
      
        private void ImageTotalExpensive_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Expensive/TotalExpensive.xaml", UriKind.Relative));
        }

        private void TextBlock_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/HelpMenu.xaml", UriKind.Relative));
        }

        private void Image_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Estmatedsalesprice.xaml", UriKind.Relative));
        }

        private void txtAgentCommission_MouseLeave_1(object sender, MouseEventArgs e)
        {
            double CommissionRate = 0.0;
            double house = App.TotalPrice;
            Appsettings.AgentCommission = Convert.ToDouble(txtAgentCommission.Text);
            double commisssiondouable = Convert.ToDouble(txtAgentCommission.Text);
            CommissionRate = (house * commisssiondouable) / 100;
            txtTotalAgentCommission.Text = CommissionRate.ToString();
            Appsettings.AgentCommissionHouse = Convert.ToDouble(txtTotalAgentCommission.Text);
            TotalSalesPrice();
        }


    }


}
